package cn.ncu.controller;

import cn.ncu.entity.Comment;
import cn.ncu.service.CommentService;
import cn.ncu.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("comment")
@CrossOrigin
public class CommentController {

    @Autowired
    private CommentService commentService;

    @GetMapping("findByPage")
    public Result findCommentByPage(String articleid, Integer currentPage, Integer rows){
        Result result = new Result();
        Integer total = commentService.findTotal(articleid);
        if(total>0){
            List<Comment> comments = commentService.findByPage(articleid,currentPage,rows);
            result.setFlag(true).setComments(comments).setTotal(total);
        }else {
            result.setMessage("分页查询失败！");
        }
        return result;
    }

    @PostMapping("saveComment")
    public Result saveComment(@RequestBody Comment comment){
        Result result = new Result();
        comment.setCreatedtime(new Date());
        try {
            commentService.saveComment(comment);
            result.setFlag(true);
        } catch (Exception e) {
            result.setMessage("评论失败，请稍后重试哦！");
        }
        return result;
    }
}
